package com.ruoyi.web.controller.biz;

import java.util.List;
import java.util.Map;

import com.alibaba.druid.support.json.JSONUtils;
import com.ruoyi.biz.service.IBizCourseDetailService;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.domain.BizCourseExam;
import com.ruoyi.biz.service.IBizCourseExamService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程考试题Controller
 * 
 * @author ruoyi
 * @date 2021-08-24
 */
@Controller
@RequestMapping("/biz/exam")
public class BizCourseExamController extends BaseController
{
    private String prefix = "biz/exam";

    @Autowired
    private IBizCourseExamService bizCourseExamService;

    @Autowired
    private IBizCourseDetailService bizCourseDetailService;

    @RequiresPermissions("biz:exam:view")
    @GetMapping()
    public String exam()
    {
        return prefix + "/exam";
    }

    /**
     * 查询课程考试题列表
     */
    @RequiresPermissions("biz:exam:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizCourseExam bizCourseExam)
    {
        startPage();
        List<BizCourseExam> list = bizCourseExamService.selectBizCourseExamList(bizCourseExam);
        return getDataTable(list);
    }

    /**
     * 导出课程考试题列表
     */
    @RequiresPermissions("biz:exam:export")
    @Log(title = "课程考试题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizCourseExam bizCourseExam)
    {
        List<BizCourseExam> list = bizCourseExamService.selectBizCourseExamList(bizCourseExam);
        ExcelUtil<BizCourseExam> util = new ExcelUtil<BizCourseExam>(BizCourseExam.class);
        return util.exportExcel(list, "exam");
    }

    /**
     * 新增课程考试题
     */
    @GetMapping({"/add/{id}","/add"})
    public String add(@PathVariable(value = "id", required = false) Long id,ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("courseDetail", bizCourseDetailService.selectBizCourseDetailById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存课程考试题
     */
    @RequiresPermissions("biz:exam:add")
    @Log(title = "课程考试题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizCourseExam bizCourseExam)
    {
        return toAjax(bizCourseExamService.insertBizCourseExam(bizCourseExam));
    }

    /**
     * 修改课程考试题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BizCourseExam bizCourseExam = bizCourseExamService.selectBizCourseExamById(id);
        mmap.put("bizCourseExam", bizCourseExam);
        return prefix + "/edit";
    }

    @GetMapping("/preview/{id}")
    public String preview(@PathVariable("id") Long id, ModelMap mmap)
    {
        BizCourseExam bizCourseExam = bizCourseExamService.selectBizCourseExamById(id);

        String examContent = bizCourseExam.getExamContent();
        if (!StringUtils.isEmpty(examContent)) {
            List<Map> List = (java.util.List<Map>) JSONUtils.parse(examContent);
            bizCourseExam.setExamList(List);
        }

        mmap.put("bizCourseExam", bizCourseExam);
        return prefix + "/preview";
    }

    /**
     * 修改保存课程考试题
     */
    @RequiresPermissions("biz:exam:edit")
    @Log(title = "课程考试题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizCourseExam bizCourseExam)
    {
        return toAjax(bizCourseExamService.updateBizCourseExam(bizCourseExam));
    }

    /**
     * 删除课程考试题
     */
    @RequiresPermissions("biz:exam:remove")
    @Log(title = "课程考试题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizCourseExamService.deleteBizCourseExamByIds(ids));
    }

    @GetMapping(value = { "/selectManageTree/{id}", "/selectManageTree/" })
    public String selectManageTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("courseDetail", bizCourseExamService.selectBizCourseExamById(id));
        }
        return prefix + "/tree";
    }

    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = bizCourseExamService.selectCourseTree();
        return ztrees;
    }

    @PostMapping("/commitExam")
    @ResponseBody
    public AjaxResult commitExam(long id,String answers)
    {
        Long userId = ShiroUtils.getUserId();
        Map result = bizCourseExamService.checkAnswer(id, answers,userId);
        return AjaxResult.success(result);
    }
}
